<?php
namespace app\home\controller;

use app\home\service\DoubanService;
use app\home\service\HotProcessService;
use app\common\service\EsSearchService;

class Hot extends \think\Controller
{
    public function index()
    {
        $sort = $this->request->get('sort', 'time');
        $tag = $this->request->get('tag', '热门');
        $playable = $this->request->get('playable', 0, 'intval');

        $process = new HotProcessService();
        $res = $process->getList(0, $sort, $tag, $playable);

        $this->assign('films', $res['list']);
        $this->assign('chart', $res['chart']);
        $this->assign('tags', DoubanService::$tag);
        $this->assign('sorts', DoubanService::$sort_type);
        $this->assign('query', ['sort' => $sort, 'tag' => $tag, 'playable' => $playable]);

        $this->assign('meta_desc', '热门电影频道,BTFILM专业电影搜索引擎,海量电影等你来搜');
        $this->assign('meta_keywords', 'BTFILM,磁力搜索,种子搜索,电影搜索,最新电影,热门电影');
        $this->assign('meta_title', '热门电影_磁力搜索_种子搜索_影视搜索_电影搜索_最新电影_BTFILM搜索');
        return $this->fetch();
    }

    public function mlist()
    {
        $sort = $this->request->get('sort', 'time');
        $tag = $this->request->get('tag', '热门');
        $playable = $this->request->get('playable', 0, 'intval');
        $start = $this->request->get('start', 0, 'intval');

        $process = new HotProcessService();
        $res = $process->getList($start, $sort, $tag, $playable);
        $this->assign('films', $res['list']);
        return $this->fetch();
    }

    public function detail($id = 0) {
        empty($id) && $id = $this->request->get('id', 0, 'intval');
        $process = new HotProcessService();
        $film = $process->getDetail_v2($id);
        if ($film->isEmpty()) {
            return error_404_page();
        }

        $es = new EsSearchService();
        $keywords = preg_replace('/(\d+)/','', $film['title']);
        if ($keywords){
            $keywords = explode(' ',$keywords);
            $keywords = $keywords[0];
        }
        $res = $es->search($keywords, 1, 10);

        $this->assign('ratings', $process->formatRatings($film->rating));
        $this->assign('film', $film);
        $this->assign('sources', $res['films']);
        $this->assign('keywords', $keywords);

        $this->assign('meta_desc', $keywords . '电影资源,BTFILM专业电影搜索引擎,海量电影等你来搜,BTFILM提供磁力搜索、种子搜索、影视搜索、电影搜索、最新电影等搜索服务');
        $this->assign('meta_keywords', $keywords . '_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_最新电影_BTFILM搜索');
        $this->assign('meta_title', $keywords . '_BTFILM_磁力搜索_种子搜索_影视搜索_电影搜索_最新电影_BTFILM搜索');

        header('Cache-control: max-age=86400');

        return $this->fetch();
    }
}
